package logic

import (
	"context"
	"github.com/SupenBysz/gf-admin-community/sys_model"
	"github.com/SupenBysz/gf-admin-community/sys_model/sys_enum"
	gf_sys"github.com/SupenBysz/gf-admin-community/sys_service"
	"github.com/SupenBysz/gf-admin-company-modules/co_model"
	mo_sys "github.com/SupenBysz/gf-admin-company-modules/co_module"
	"github.com/gogf/gf/v2/util/gconv"
	"net/http"
	"server/api"
)

type SchoolLogic struct {}

func NewSchoolLogic() *SchoolLogic {
	return &SchoolLogic{
	}
}

// CreateStudent 添加学生
func (s *SchoolLogic) CreateStudent (ctx context.Context, data *api.CreateMemberReq)(resp *api.Response,err error){
	resp = &api.Response{}

	//调用项目一注册
	// 创建登录信息
	user, err := gf_sys.SysUser().CreateUser(ctx, sys_model.UserInnerRegister{
		Username:        data.Name,
		RoleIds:         []int64{data.UnionUserId},
		Mobile:          gconv.String(data.Mobile),
	},
		sys_enum.UserState(sys_enum.User.State.Normal),
		sys_enum.UserType(sys_enum.User.Type.New(1,"Student")),
	)
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}

	// 调用项目二插入员工信息
	modules := mo_sys.Modules{}
	account,err:=modules.Account().CreateAccount(ctx,co_model.FdAccountRegister{
		Name:               data.Name,
		UnionUserId:        data.UnionUserId,
		IsEnabled:          1,
		LimitState:         0,
	})
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}

	//创建返回信息

	resp.Data = struct {
		User any `json:"user"`
		Account any `json:"account"`
	}{
		User:    user,
		Account: account,
	}

	return

}

// CreateTeacher 添加老师
func (s *SchoolLogic) CreateTeacher (ctx context.Context, data *api.CreateMemberReq)(resp *api.Response,err error){
	resp = &api.Response{}

	//调用项目一注册
	// 创建登录信息
	newUser, err := gf_sys.SysUser().CreateUser(ctx, sys_model.UserInnerRegister{
		Username:        data.Name,
		Mobile:          gconv.String(data.Mobile),
	},
		sys_enum.UserState(sys_enum.User.State.Normal),
		sys_enum.UserType(sys_enum.User.Type.New(2,"Teacher")),
	)
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}


	// 调用项目二插入员工信息
	modules := mo_sys.Modules{}
	account,err:=modules.Account().CreateAccount(ctx,co_model.FdAccountRegister{
		Name:               data.Name,
		IsEnabled:          1,
		LimitState:         0,
	})
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}


	//插入班级信息
	team,err:=modules.Team().CreateTeam(ctx,&co_model.Team{
		Name:              data.Name + "的班级",
		OwnerEmployeeId:   newUser.Id,
	})
	if err != nil {
		resp.Msg = err.Error()
		resp.Code= http.StatusInternalServerError
		return
	}

	//创建返回信息

	resp.Data = struct {
		User any `json:"user"`
		Account any `json:"account"`
		Team any `json:"team"`
	}{
		User:    newUser,
		Account: account,
		Team: team,
	}

	return

}


// QueryMember 人员查询 ID
func (s *SchoolLogic) QueryMemberById (ctx context.Context, data *api.QueryMemberByIdReq)(resp *api.Response,err error){
	resp = &api.Response{}

	//user
	user,err:=gf_sys.SysUser().GetUserDetail(ctx,data.Id)
	if err != nil {
		resp.Msg = err.Error()
		resp.Code= http.StatusInternalServerError
		return
	}

	// 调用项目二插入员工信息
	modules := mo_sys.Modules{}
	account,err:=modules.Account().GetAccountById(ctx,data.Id)
	if err != nil {
		resp.Msg = "error_Employee_User_Save_Failed"
		resp.Code= http.StatusInternalServerError
		return
	}

	//插入班级信息
	team,err:=modules.Team().GetTeamByName(ctx,user.Username+"的班级")
	if err != nil {
		resp.Msg = err.Error()
		resp.Code= http.StatusInternalServerError
		return
	}

	//创建返回信息

	resp.Data = struct {
		User any `json:"user"`
		Account any `json:"account"`
		Team any `json:"team"`
	}{
		User:    user,
		Account: account,
		Team: team,
	}

	return
}